﻿Option Explicit On
Option Strict On

Partial Class private_RegRole
    Inherits System.Web.UI.Page
    Dim u As MembershipUser
    Protected Sub Button1_Click(sender As Object, e As System.EventArgs) Handles Button1.Click
        'Now write the result to the dbo.UserRole Table for this user
        'Only one will be set so write the one that is set to the dbo.UserRole Table
        'Write to the Client_ContactMe table when the user clicks the rboContactMe button.
        'Also send an email to the administrator
        If rbFullOwner1.Checked Then
            HandleRoles("ListOwnerSingle", "FullOwner")
        End If
        If rbFullOwner2.Checked Then
            HandleRoles("ListOwnerMulti", "MultiOwner")
        End If
        If rbAssociate.Checked Then
            HandleRoles("ListAssociate", "ListAssociate")
        End If
        If rbListAgent.Checked Then
            HandleRoles("ListAgent", "ListAgent")
        End If
        If rbContactMe.Checked Then
            HandleRoles("SiteVisitor", "SiteVisitor")
            'Response.Redirect("")
        End If

    End Sub
    Sub HandleRoles(ByVal userrole As String, ByVal memrole As String)
        Try
            u = Membership.GetUser(User.Identity.Name)
            Dim personTA As New LogginsLogDSTableAdapters.PersonTableAdapter()
            Dim personDT As LogginsLogDS.PersonDataTable
            personDT = personTA.GetData(CType(u.ProviderUserKey, Global.System.Guid?))
            Dim personid As Integer? = personTA.GetData(CType(u.ProviderUserKey, Global.System.Guid?)).FirstOrDefault().Id

            Dim personsiteroleTA As New LogginsLogDSTableAdapters.usp_PersonSiteRoleTableAdapter()
            Dim personsiteroleDT As LogginsLogDS.usp_PersonSiteRoleDataTable
            personsiteroleDT = personsiteroleTA.GetData(personid)
            Dim personsiteroleID As Integer?

            If personsiteroleDT.Rows.Count() <> 0 Then
                For Each personsiteroleRow As LogginsLogDS.usp_PersonSiteRoleRow In personsiteroleDT
                    personsiteroleID = personsiteroleRow.Id
                Next
            End If
            If personsiteroleDT.Rows.Count = 0 Then
                Dim newpersonsiteroleTA As New LogginsLogDSTableAdapters.usp_PersonSiteRoleTableAdapter()
                Dim newnow As Integer = newpersonsiteroleTA.Insert(personid, False, False, False, False, True, personsiteroleID)
            Else
                personsiteroleTA.Update(personid, False, False, False, False, True, personid)
            End If

            'personsiteroleID = personsiteroleTA.Fill(personid)

            If User.IsInRole("FullOwner") Then
                Roles.RemoveUserFromRole(User.Identity.Name, "FullOwner")
            End If
            If User.IsInRole("MultiOwner") Then
                Roles.RemoveUserFromRole(User.Identity.Name, "MultiOwner")
            End If
            If User.IsInRole("ListAssociate") Then
                Roles.RemoveUserFromRole(User.Identity.Name, "ListAssociate")
            End If
            If User.IsInRole("ListAgent") Then
                Roles.RemoveUserFromRole(User.Identity.Name, "ListAgent")
            End If
            If User.IsInRole("SiteVisitor") Then
                Roles.RemoveUserFromRole(User.Identity.Name, "SiteVisitor")
            End If

            Roles.AddUserToRole(User.Identity.Name, memrole)
            If User.IsInRole("FullOwner") Then
                personsiteroleTA.Update(personid, True, False, False, False, True, personsiteroleID)
            End If
            If User.IsInRole("MultiOwner") Then
                personsiteroleTA.Update(personid, False, True, False, False, True, personsiteroleID)
            End If
            If User.IsInRole("ListAssociate") Then
                personsiteroleTA.Update(personid, False, False, True, False, True, personsiteroleID)
            End If
            If User.IsInRole("ListAgent") Then
                personsiteroleTA.Update(personid, False, False, False, True, True, personsiteroleID)
            End If
            If User.IsInRole("SiteVisitor") Then
                Dim client_contactmeTA As New LogginsLogDSTableAdapters.usp_Client_ContactMeTableAdapter()
                client_contactmeTA.Insert(personid, True)
            End If


        Catch ex As Exception

        Finally
            Dim addnewusercheckTA As New LogginsLogDSTableAdapters.SPTableAdapter()
            Try
                addnewusercheckTA.usp_UserCheckUserRoleSetUpdate(CType(u.ProviderUserKey, Global.System.Guid?), True)
            Catch ex As Exception
                Throw ex
            Finally

                Dim SendEmail As New SendEmailViaGoogle()
                Dim strMailFrom As String = ConfigurationManager.AppSettings("smtpmailfrom").ToString()
                Dim strMailTo As String = ConfigurationManager.AppSettings("accountapprovalemail").ToString()

                Try
                    SendEmail.SendEmail(strMailFrom, strMailTo, "User:" + User.Identity.Name, " ... filled in Site Role ")

                    'if we get here all is well
                    Response.Redirect("~/private/default.aspx")

                Catch ex As Exception
                    Throw ex
                End Try

            End Try

        End Try
    End Sub


End Class
